<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/hello' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-button type="primary" @click="showaddCate()">添加分类</el-button>
      </el-row>
      <div>
        <zk-table
          ref="table"
          :data="data"
          :columns="columns"
          index-text="#"
          show-index
          border
          :is-fold="true"
          :expand-type="false"
          :selection-type="false"
        >
          <template slot="isdelete" scope="scope">
            <i style="color:#67C23A" class="el-icon-success" v-if="scope.row.cat_deleted == 0"></i>
            <i style="color:#F56C6C" class="el-icon-error" v-if="scope.row.cat_deleted == 1"></i>
          </template>
          <template slot="level" scope="scope">
            <el-button size="mini" plain type="success" v-if="scope.row.cat_level ==0">一级</el-button>
            <el-button size="mini" plain type="info" v-if="scope.row.cat_level ==1">二级</el-button>
            <el-button size="mini" plain type="warning" v-if="scope.row.cat_level ==2">三级</el-button>
          </template>
          <template slot="operate">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="editCategory(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteCategory(scope.row)"
            ></el-button>
          </template>
        </zk-table>
      </div>
      <!-- 添加分类对话框 -->
      <el-dialog title="添加分类" :visible.sync="addCateVisible" :before-close="closeAdd">
        <el-form :model="addCateFrom" :rules="rules" ref="addCateFromRef" >
          <el-form-item label="分类名称：" prop="cat_name" label-width="100px">
            <el-input v-model="addCateFrom.cat_name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="父级分类：" label-width="100px">
            <el-cascader
              v-model="addPid"
              :options="cateList"
              :props="addCateprop"
              @change="handleChange"
              change-on-select
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAdd">取 消</el-button>
          <el-button type="primary" @click="addCategory()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 请求分类列表参数
      queryInfo: {
        type: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类分页数据存放
      data: [],
      total: 0,
      columns: [
        {
          label: '商品分类',
          prop: 'cat_name'
          //   width:'100px'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isdelete'
          //   width:'100px'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'level'
          //   width:'100px'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operate'
        }
      ],
      // 联级选择器获取所有分类数据
      cateList: [],
      // 联级选择器配置
      addCateprop: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 请求添加分类参数
      addCateVisible: false,
      addPid: [],
      addCateFrom: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      rules: {
        cat_name: [{ required: true, message: '请输入合法的名称', trigger: 'blur' }, { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }]
      }
    }
  },

  methods: {
    // 获取分类列表
    async Goodscategoris() {
      let { data: res } = await this.$Http.getGoodsCategory(this.queryInfo)
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.data = res.data.result
      this.total = res.data.total
    },
    // 改变pagesize
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.Goodscategoris()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.Goodscategoris()
    },
    // 点击编辑
    editCategory(val) {},
    // 点击删除
    deleteCategory(val) {},
    // 添加分类对话框
    async showaddCate() {
      this.addCateVisible = true
      let { data: res } = await this.$Http.getGoodsCategory({ type: 2 })
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.cateList = res.data
    },
    // 监听联级选择器变化
    handleChange(val) {
      this.addPid = val
      this.addCateFrom.cat_level = val.length
      this.addCateFrom.cat_pid = val[val.length - 1]
      console.log(this.addCateFrom)
    },
    // 添加分类完成
    closeAdd() {
      this.addCateVisible = false
      this.$refs.addCateFromRef.resetFields()
      this.addPid = []
    },
    async addCategory() {
      this.addCateVisible = false
      let { data: res } = await this.$Http.addCategoris(this.addCateFrom)
      if (res.meta.status != 201) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.Goodscategoris()
      this.closeAdd()
    }
  },

  created() {
    this.Goodscategoris()
    // this.$refs.addCateFromRef.resetFields()
  }
}
</script>
<style lang='less' scoped>
.zk-table {
  margin-top: 10px;
  font-size: 16px;
}
.el-cascader {
  width: 100%;
}
</style>
